package com.king.pattem.pool;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool.PoolableObjectFactory;

/**
 * 对象工厂
 * @author taomk
 * @version 1.0
 * @since 15-1-17 下午4:32
 */
public class PoolableObjectFactoryDemo implements PoolableObjectFactory {

	private static AtomicInteger counter = new AtomicInteger(0);

	/**
	 * 创建对象
	 * @return
	 * @throws Exception
	 */
	@Override
	public Object makeObject() throws Exception {
		Object object = String.valueOf(counter.getAndIncrement());
		System.out.println("Create Object " + object);
		return object;
	}

	/**
	 * 销毁对象
	 * @param obj
	 * @throws Exception
	 */
	@Override
	public void destroyObject(Object obj) throws Exception {
		System.out.println("Destroy Object " + obj);
	}

	/**
	 * 对象是否可用
	 * @param obj
	 * @return
	 */
	@Override
	public boolean validateObject(Object obj) {
		return true;
	}

	/**
	 * 取出前调用
	 * @param obj
	 * @throws Exception
	 */
	@Override
	public void activateObject(Object obj) throws Exception {
		System.out.println("Before borrow " + obj);
	}

	/**
	 * 返回时调用
	 * @param obj
	 * @throws Exception
	 */
	@Override
	public void passivateObject(Object obj) throws Exception {
		System.out.println("return " + obj);
	}
}
